import urlConfig from '../common/config.js'

export const request=({url,methods,data,dataType})=>{ 
	//登录拦截，仍需捕获异常
	//注意登录接口不可调用该公共方法，否则陷入递归循环
	let header = {}
	 try {
	       const token = uni.getStorageSync('code');
		   // const token = '043rR1ll2Y8S564CW3ol2GbTZS1rR1lH'
	       if (token) {
			 if(methods == 'GET' || !methods) {
				 header = {'jsCode' : token,'X-Requested-With': 'XMLHttpRequest',"Accept": "application/json","Content-Type": "application/json; charset=UTF-8"}
			 }else {
				 header = {'jsCode' : token,'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/json; charset=UTF-8'}
			 }
	        }else{
	            uni.navigateTo({
	                url: '../pages/login/login'   
	            });
	            return;
	        }
	     } catch (err) {
	      console.log(err)
	    }
	return new Promise((resolve,reject)=>{
		uni.request({
			url:urlConfig+url,
			method:methods||"GET",
			data:data || {},
			header,
			dataType:dataType || "json",
			success: (res) => {
				if(res.data.ok == false){
					return uni.showToast({
						icon:'none',
						title:res.data.message || '获取数据失败'
					})
				}else resolve(res.data)
				
			},
			fail: (err) => {
				uni.showToast({
					icon:'none',
					title:"接口请求失败"
				})
				reject(err)
			}
		})
	})
}